
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>Tk图形用户界面(GUI) &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="tkinter --- Tcl/Tk的Python接口" href="tkinter.html" />
    <link rel="prev" title="shlex --- Simple lexical analysis" href="shlex.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/tk.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="tkinter.html" title="tkinter --- Tcl/Tk的Python接口"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="shlex.html" title="shlex --- Simple lexical analysis"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python 标准库</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="graphical-user-interfaces-with-tk">
<span id="tkinter"></span><h1>Tk图形用户界面(GUI)<a class="headerlink" href="#graphical-user-interfaces-with-tk" title="永久链接至标题">¶</a></h1>
<p id="index-0">Tcl/Tk集成到Python中已经有一些年头了。Python程序员可以通过 <a class="reference internal" href="tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> 包和它的扩展， <a class="reference internal" href="tkinter.tix.html#module-tkinter.tix" title="tkinter.tix: Tk Extension Widgets for Tkinter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.tix</span></code></a> 模块和 <a class="reference internal" href="tkinter.ttk.html#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.ttk</span></code></a> 模块，来使用这套鲁棒的、平台无关的窗口工具集。</p>
<p><a class="reference internal" href="tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> 包使用面向对象的方式对Tcl/Tk进行了一层薄包装。使用 <a class="reference internal" href="tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> ，你不需要写Tcl代码，但可能需要参考Tk文档，甚至Tcl文档。 <a class="reference internal" href="tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> 使用Python类，对Tk的窗体小部件（Widgets）进行了一系列的封装。除此之外，内部模块 <code class="xref py py-mod docutils literal notranslate"><span class="pre">_tkinter</span></code> 针对Python和Tcl之间的交互，提供了一套线程安全的机制。</p>
<p><a class="reference internal" href="tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> 最大的优点就一个字：快，再一个，是Python自带的。尽管官方文档不太完整，但有其他资源可以参考，比如Tk手册，教程等。 <a class="reference internal" href="tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> 也以比较过时的外观为人所知，但在Tk 8.5中，这一点得到了极大的改观。除此之外，如果有兴趣，还有其他的一些GUI库可供使用。更多信息，请参考 <a class="reference internal" href="othergui.html#other-gui-packages"><span class="std std-ref">其他图形用户界面（GUI）包</span></a>  小节。</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="tkinter.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> --- Tcl/Tk的Python接口</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tkinter.html#tkinter-modules">Tkinter 模块</a></li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.html#tkinter-life-preserver">Tkinter Life Preserver</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#how-to-use-this-section">How To Use This Section</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#a-simple-hello-world-program">A Simple Hello World Program</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.html#a-very-quick-look-at-tcl-tk">A (Very) Quick Look at Tcl/Tk</a></li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.html#mapping-basic-tk-into-tkinter">Mapping Basic Tk into Tkinter</a></li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.html#how-tk-and-tkinter-are-related">How Tk and Tkinter are Related</a></li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.html#handy-reference">Handy Reference</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#setting-options">Setting Options</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#the-packer">The Packer</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#packer-options">Packer Options</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#coupling-widget-variables">Coupling Widget Variables</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#the-window-manager">The Window Manager</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#tk-option-data-types">Tk Option Data Types</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#bindings-and-events">Bindings and Events</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#the-index-parameter">The index Parameter</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.html#images">Images</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.html#file-handlers">File Handlers</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tkinter.ttk.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.ttk</span></code> --- Tk主题小部件</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#using-ttk">使用 Ttk</a></li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#ttk-widgets">Ttk 部件</a></li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#widget">控件</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#standard-options">标准选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#scrollable-widget-options">可滚动控件选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#label-options">标签选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#compatibility-options">兼容性选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#widget-states">控件状态</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#ttk-widget">ttk.Widget</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#combobox">组合框</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#options">选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#virtual-events">虚拟事件</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#ttk-combobox">ttk.Combobox</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#spinbox">Spinbox</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#id1">选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#id2">虚拟事件</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#ttk-spinbox">ttk.Spinbox</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#notebook">笔记本</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#id3">选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#tab-options">Tab 选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#tab-identifiers">Tab Identifiers</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#id4">Virtual Events</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#ttk-notebook">ttk.Notebook</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#progressbar">Progressbar</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#id5">选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#ttk-progressbar">ttk.Progressbar</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#separator">Separator</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#id6">选项</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#sizegrip">Sizegrip</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#platform-specific-notes">Platform-specific notes</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#bugs">Bugs</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#treeview">Treeview</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#id7">选项</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#item-options">Item Options</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#tag-options">Tag Options</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#column-identifiers">Column Identifiers</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#id8">Virtual Events</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#ttk-treeview">ttk.Treeview</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.ttk.html#ttk-styling">Ttk Styling</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.ttk.html#layouts">Layouts</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tkinter.tix.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.tix</span></code> --- Extension widgets for Tk</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tkinter.tix.html#using-tix">Using Tix</a></li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.tix.html#tix-widgets">Tix Widgets</a><ul>
<li class="toctree-l3"><a class="reference internal" href="tkinter.tix.html#basic-widgets">Basic Widgets</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.tix.html#file-selectors">File Selectors</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.tix.html#hierarchical-listbox">Hierarchical ListBox</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.tix.html#tabular-listbox">Tabular ListBox</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.tix.html#manager-widgets">Manager Widgets</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.tix.html#image-types">Image Types</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.tix.html#miscellaneous-widgets">Miscellaneous Widgets</a></li>
<li class="toctree-l3"><a class="reference internal" href="tkinter.tix.html#form-geometry-manager">Form Geometry Manager</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="tkinter.tix.html#tix-commands">Tix Commands</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tkinter.scrolledtext.html"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.scrolledtext</span></code> --- 滚动文字控件</a></li>
<li class="toctree-l1"><a class="reference internal" href="idle.html">IDLE</a><ul>
<li class="toctree-l2"><a class="reference internal" href="idle.html#menus">目录</a><ul>
<li class="toctree-l3"><a class="reference internal" href="idle.html#file-menu-shell-and-editor">文件目录 （命令行和编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#edit-menu-shell-and-editor">编辑目录（命令行和编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#format-menu-editor-window-only">格式菜单（仅 window 编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#run-menu-editor-window-only">运行菜单（仅 window 编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#shell-menu-shell-window-only">Shell 菜单（仅 window 编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#debug-menu-shell-window-only">调试菜单（仅 window 编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#options-menu-shell-and-editor">选项菜单（命令行和编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#window-menu-shell-and-editor">Window 菜单（命令行和编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#help-menu-shell-and-editor">帮助菜单（命令行和编辑器）</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#context-menus">上下文菜单</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="idle.html#editing-and-navigation">编辑和导航</a><ul>
<li class="toctree-l3"><a class="reference internal" href="idle.html#editor-windows">编辑窗口</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#key-bindings">按键绑定</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#automatic-indentation">自动缩进</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#completions">完成</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#calltips">提示</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#code-context">代码上下文</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#python-shell-window">Python Shell 窗口</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#text-colors">文本颜色</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="idle.html#startup-and-code-execution">启动和代码执行</a><ul>
<li class="toctree-l3"><a class="reference internal" href="idle.html#command-line-usage">命令行语法</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#startup-failure">启动失败</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#running-user-code">运行用户代码</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#user-output-in-shell">Shell中的用户输出</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#developing-tkinter-applications">开发 tkinter 应用程序</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#running-without-a-subprocess">在没有子进程的情况下运行</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="idle.html#help-and-preferences">帮助和偏好</a><ul>
<li class="toctree-l3"><a class="reference internal" href="idle.html#help-sources">帮助资源</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#setting-preferences">偏好设定</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#idle-on-macos">macOS 上的IDLE</a></li>
<li class="toctree-l3"><a class="reference internal" href="idle.html#extensions">扩展</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="othergui.html">其他图形用户界面（GUI）包</a></li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="shlex.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code> --- Simple lexical analysis</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="tkinter.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> --- Tcl/Tk的Python接口</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/tk.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="tkinter.html" title="tkinter --- Tcl/Tk的Python接口"
             >下一页</a> |</li>
        <li class="right" >
          <a href="shlex.html" title="shlex --- Simple lexical analysis"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>